//
//  TimeToOverView.swift
//  ShareMusic
//
//  Created by 胡岩 on 16/5/12.
//  Copyright © 2016年 胡岩. All rights reserved.
//

import UIKit

class TimeToOverView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
    
    var pickView:UIPickerView?
    var placeholder:UIView?
    var conform:UIButton?
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        placeholder = UIView.init(frame: UIScreen.mainScreen().bounds)
        self.addSubview(placeholder!)
        placeholder?.backgroundColor = UIColor.blackColor()
        placeholder?.alpha = 0.0
        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(self.close))
        placeholder?.addGestureRecognizer(tap)
        pickView = UIPickerView.init(frame: CGRectMake(0, CGRect.screenHeight() - 400, CGRect.screenWidth(), 200))
        pickView?.delegate = self
        pickView?.dataSource = self
        self.addSubview(pickView!)
        
        conform = UIButton.init(type: UIButtonType.Custom)
        conform?.frame = CGRectMake(CGRect.screenWidth() - 60, 30, 40, 40)
        conform?.setTitle("确定", forState: UIControlState.Normal)
        conform?.addTarget(self, action: #selector(self.timerStart), forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(conform!)
    }
    
    func show() {
        let window:UIWindow = UIApplication.sharedApplication().keyWindow!
        window.addSubview(self)
        UIView.animateWithDuration(0.3) { 
            self.placeholder?.alpha = 0.6
        }
    }
    
    func close() {
        UIView.animateWithDuration(0.3, animations: { 
            self.placeholder?.alpha = 0.0
            }) { (finashed) in
                self.removeFromSuperview()
        }
    }
    
    func timerStart() {
        let second:Int = (pickView?.selectedRowInComponent(0))! * 60
        SMMusicPlayer.shareInstance().startStopTimer(Double(second))
        self.close()
    }
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 60
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "\(row)分"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
